﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public sealed class FoxControl : BasePlayerControl
{
    #region 方便用的数值

    public const string jump = "jump";
    public const string left = "left";
    public const string right = "right";
    public const string run="run";

    private bool isGround;
    public bool IsGround
    {
        get { return isGround; }
        set
        {
            isGround = value;
            foxAnimator.SetBool("OnGround", value);
        }
    }

	public float Speed{
		get{
			return _speed;
		}
		set{
			if(value==_runSpeed)
			{
				foxAnimator.SetBool("IsRunning",true);
			}
			else{
				foxAnimator.SetBool("IsRunning",false);
			}
			_speed=value;
		}
	}

    Rigidbody2D foxRigidbody2D;
    Animator foxAnimator;
    Vector3 nowScale;
    #endregion

    #region 小狐狐控制函数
    protected override void Attack()
    {

    }
    #region 控制跳跃相关
    float intervalTime;
    protected override void Jump()
    {
        if (Input.GetKeyDown(ControlKeyCode[jump]) && IsGround)
        {
            Vector2 v = foxRigidbody2D.velocity;
            v.y = jumpSpeed;
            foxRigidbody2D.velocity = v;
            foxAnimator.SetTrigger("JumpUp");
            GetComponent<AudioSource>().Play();
            intervalTime = 0f;
        }
        else if (Input.GetKey(ControlKeyCode[jump]) && (foxRigidbody2D.velocity.y > jumpSpeed - 15))
        {
            if (intervalTime < 0.3f)
            {
                intervalTime += Time.deltaTime;
                Vector2 v = foxRigidbody2D.velocity;
                v.y = jumpSpeed;
                foxRigidbody2D.velocity = v;
            }
        }
        if (foxRigidbody2D.velocity.y < 0)
        {
            foxAnimator.SetTrigger("JumpDown");
        }
    }
    #endregion
    #region 控制移动相关
    protected override void Walk()
    {
		if (Input.GetKey(ControlKeyCode[right]))
        {
            if (nowScale.x != 1)
            {
                nowScale.x *= -1;
                transform.localScale = nowScale;
            }
            Vector2 v = foxRigidbody2D.velocity;
            v.x = _speed;
            foxRigidbody2D.velocity = v;
        }
        else if (Input.GetKey(ControlKeyCode[left]))
        {
            if (nowScale.x != -1)
            {
                nowScale.x *= -1;
                transform.localScale = nowScale;
            }
            Vector2 v = foxRigidbody2D.velocity;
            v.x = -_speed;
            foxRigidbody2D.velocity = v;
        }
        if (Mathf.Abs(foxRigidbody2D.velocity.x) < _speed - 1)
        {
            foxAnimator.SetBool("IsWalk", false);
			Speed=10f;
        }
        else
        {
            foxAnimator.SetBool("IsWalk", true);
        }
    }

    private void Run()
    {
        if (Speed!=_runSpeed)
        {
            if (Input.GetKey(ControlKeyCode[run]))
            {
				Speed=_runSpeed;
            }
            
        }
        if(Input.GetKeyUp(ControlKeyCode[run]))
        {
           Speed=10;
        }
    }

    #endregion

    void Start()
    {
        for (int i = 0; i < ControlKeys.Length; i++)
        {
            ControlKeyCode.Add(KeysEffects[i], ControlKeys[i]);
        }
        nowScale = transform.localScale;
        foxRigidbody2D = transform.parent.gameObject.GetComponent<Rigidbody2D>();
        foxAnimator = GetComponent<Animator>();
        IsGround = true;
    }

    void Update()
    {
        Walk();
        Jump();
        Run();
    }
    #endregion

}
